"use strict";
/**
 * @name dau
 * 角色登录,DAU统计
 */

/**
 * @name /
 * @param int sid 当前登录服务器ID
 * @param int time 注册时间
 * 角色登录,收集DAU
 */

module.exports = function() {
    let sid = this.get('sid','int') || 0;
    let time = this.get('time','int') || 0;
    if ( !sid ) {
        return this.error('sid empty');
    }
    if ( !time  ) {
        return this.error('time empty');
    }
    let today = this.library("time/today")
    let dauModel = this.model.branch("dau",this.branch,sid)
    //DAU
    dauModel.incr(today,'dau',1);
    //DRU
    let regDate = new Date(time);
    regDate.setHours(0,0,0,0);
    let regTime = regDate.getTime();
    if(regTime > today){
        return this.success('ok');
    }

    let DAYMS = 86400 * 1000;
    let i = Math.floor((today - regTime) / DAYMS) + 1;
    let dauConfig = this.config("dau");
    for(let k in dauConfig){
        if(dauConfig[k]['limit'][0] >= i && dauConfig[k]['limit'][1] <= i ) {
            dauModel.incr(time,k,1);
        }
    }
}